package com.ly.drivingschool.controller;

import com.ly.drivingschool.common.ResponseMessage;
import com.ly.drivingschool.pojo.Dict;
import com.ly.drivingschool.pojo.Pager;
import com.ly.drivingschool.pojo.Setmeal;
import com.ly.drivingschool.pojo.Staff;
import com.ly.drivingschool.service.DictService;
import com.ly.drivingschool.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

/**
 * 套餐管理
 */
@Slf4j
@RestController
@RequestMapping(value = "setmeal",produces = "application/json; charset=utf-8")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DictService dictService;

    /**
     * 分页查询驾校员工信息
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "getPageSetmealInfo")
    public ResponseMessage getPageSetmealInfo(int page, int limit) {
        Pager<Setmeal> pager = setmealService.findByPager(page,limit);
        System.out.println(pager);
        if (pager.getCount() == 0) {
            return ResponseMessage.createBySuccess("驾校暂无该套餐信息",pager);
        } else {
            return ResponseMessage.createBySuccess(pager);
        }
    }

    /**
     * 模糊查询
     * @param page
     * @param limit
     * @param keyWork
     * @return
     */
    @GetMapping(value = "getKeyWorkSetmealInfo")
    public ResponseMessage getKeyWorkSetmealInfo(int page,int limit, String keyWork){
        Pager<Setmeal> pager = setmealService.findByKey(page,limit,keyWork);
        if (pager.getCount() == 0) {
            return ResponseMessage.createBySuccess("驾校没有该套餐信息,请检查",pager);
        } else {
            return ResponseMessage.createBySuccess(pager);
        }
    }

    /**
     * 删除一条记录
     * @param setmeal_id
     * @return
     */
    @PostMapping(value = "delByIdSetmeal")
    public ResponseMessage delByIdSetmeal(int setmeal_id) {
        int code = setmealService.deleteByPrimaryKey(setmeal_id);
        return ResponseMessage.createBySuccess(code);
    }

    /**
     * 批量删除
     * @param array
     * @return
     */
    @PostMapping(value = "delByBatchSetmeal")
    public ResponseMessage delByBatchSetmeal(int[] array) {
        int code = setmealService.deleteByBatchSetmeal(array);
        return ResponseMessage.createBySuccess(code);
    }

    /**
     * 套餐信息
     * @param setmeal_id
     * @return
     */
    @GetMapping(value = "getByIdSetmeal")
    public ResponseMessage getByIdSetmeal(int setmeal_id) {
        Setmeal setmeal = setmealService.selectByPrimaryKey(setmeal_id);
        Setmeal setmealDict = setmealService.findByDict(setmeal_id);
        System.out.println("套餐信息：" + setmeal);
        System.out.println("套餐信息value：" + setmealDict);

        List<Dict> dictList1 = dictService.getByBelong("xlc001");

//        List<Staff> staffZG001 = staffService.getStaffByZG001("021");

        HashMap<String,Object> staffHashMap = new HashMap<>();
        staffHashMap.put("setmealCode",setmeal);
        staffHashMap.put("setmealValue",setmealDict);

        // 字典值
        staffHashMap.put("xlc001",dictList1);

//        staffHashMap.put("staffZG001",staffZG001);



        if (setmeal == null) {
            return ResponseMessage.createByErrorMessage("查无此套餐");
        } else {
            return ResponseMessage.createBySuccess("查看套餐信息",staffHashMap);
        }
    }

    /**
     * 更新记录
     * @param setmeal
     * @return
     */
    @PostMapping(value = "updateByIdSetmeal")
    public ResponseMessage updateByIdStaff(@RequestBody Setmeal setmeal){
        System.out.println("获取的setmeal信息：" + setmeal.toString());
        int code = setmealService.updateByPrimaryKeySelective(setmeal);
        if (code == 0) {
            return ResponseMessage.createByErrorMessage("更新失败");
        } else {
            return ResponseMessage.createBySuccess("更新成功", code);
        }
    }

    /**
     * 添加新员工信息
     * @param setmeal
     * @return
     */
    @PostMapping(value = "addSetmealInfo")
    public ResponseMessage addStaffInfo(@RequestBody Setmeal setmeal){
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        String createTime = df.format(calendar.getTime());
        // 封装创建时间
        setmeal.setCreateTime(createTime);
        // 封装默认人数为0
        setmeal.setApplyNum("0");
        // 还得封装操作人员admin_id

        System.out.println(setmeal.toString());
        int code = setmealService.insertSelective(setmeal);
        return ResponseMessage.createBySuccess(code);
    }
}
